
const { Configuration } = require('webpack')
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const { VueLoaderPlugin } = require('vue-loader')
const Mock = require('mockjs')

/**
 * @type {Configuration}
 * */

const config = {
    mode: 'development',
    // 入口
    entry: './src/index.js',
    // 出口
    output: {
        filename: '[name].[contenthash].bundle.js',
        path: path.resolve(__dirname, './dist'),
        clean: true, // 在生成文件之前清空 output 目录
    },
    module: {
        rules: [
            {
                test: /\.s?css$/i,
                use: ['vue-style-loader', 'css-loader', 'sass-loader'],
            },
            {
                test: /\.(jpe?g|png|gif|svg)$/i,
                type: 'asset',
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader'
            }
        ],
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './public/index.html'
        }),
        new VueLoaderPlugin()
    ],
    resolve: {
        // 解析文件后缀名
        extensions: ['.js', '.vue'],
        alias: {
            '@': path.resolve(__dirname, './src')
        }
    },
    devServer: {
        port: 4000,
        open: true,
        hot: true,
        host: 'localhost',
        setupMiddlewares(middlewares, devServer){
            const { list } = Mock.mock({
                'list|10': [{
                    'id': '@id',
                    'title': '@ctitle',
                    'desc': '@cword(30,50)'
                }]
            })
            devServer.app.get('/api/list', (req, res)=>{
                res.send({
                    code: 200,
                    data: list
                })
            })

            return middlewares;
        }
    }
}

module.exports = config